package com.chenyue.cm.api.homePage;

import com.chenyue.cm.auth.Login;
import com.chenyue.cm.auth.LoginUser;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.domain.JdSearchHistory;
import com.chenyue.cm.common.service.CommonInfoService;
import com.chenyue.cm.common.service.JdSearchHistoryService;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.live.vo.LiveStudio;
import com.chenyue.cm.match.vo.Address;
import com.chenyue.cm.news.domain.JdNews;
import com.chenyue.cm.user.domain.JdAppUser;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Author: ljy
 * @Title: SearchHistory
 * @Description:
 * @Date: 2021-07-26
 */
@RestController
@RequestMapping("/api/searchHistory")
@Api(value = "搜索历史api", tags = "搜索历史api")
@ApiSort(3)
public class SearchHistoryController {

    private final JdSearchHistoryService jdSearchHistoryService;

    private final CommonInfoService commonInfoService;

    public SearchHistoryController(JdSearchHistoryService jdSearchHistoryService, CommonInfoService commonInfoService) {
        this.jdSearchHistoryService = jdSearchHistoryService;
        this.commonInfoService = commonInfoService;

    }

    /**
     * 关键词搜索直播间
     *
     * @param keyWords 搜索关键词
     * @return Success
     */
    @PostMapping("/liveSelect")
    @ApiOperation(value = "关键词搜索直播间", notes = "关键词搜索直播间")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
            @ApiImplicitParam(value = "关键词", name = "keyWords", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前城市code", name = "city", dataType = "int", required = true),
            @ApiImplicitParam(value = "登录用户id", name = "userId", dataType = "int", required = true),
    })
    public Success<Page<List<LiveStudio>>> liveSelect(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String keyWords, Integer userId, Integer city
    ) {
        Address address = new Address();
        if (city != null) {
            address = commonInfoService.getAddressByArea(city);
        }
        Page<List<LiveStudio>> list = jdSearchHistoryService.liveSelect(keyWords, page, rows, userId, address);
        return Success.success(list);
    }

    /**
     * 关键词搜索新闻
     *
     * @param keyWords 搜索关键词
     * @return Success
     */
    @PostMapping("/newsSelect")
    @ApiOperation(value = "关键词搜索新闻", notes = "关键词搜索新闻")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
            @ApiImplicitParam(value = "关键词", name = "keyWords", dataType = "int", required = true),
            @ApiImplicitParam(value = "登录用户id", name = "userId", dataType = "int", required = true),
    })
    public Success<Page<List<JdNews>>> newsSelect(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String keyWords, Integer userId
    ) {
        Page<List<JdNews>> list = jdSearchHistoryService.newsSelect(keyWords, page, rows, userId);
        return Success.success(list);
    }

    /**
     * 获取用户的搜索历史
     *
     * @return Success
     */
    @PostMapping("/getSelectList")
    @ApiOperation(value = "获取用户的搜索历史", notes = "获取用户的搜索历史")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "搜索类型（1直播间 2新闻 3赛事）", name = "type", dataType = "int", required = true),
            @ApiImplicitParam(value = "登录用户id", name = "userId", dataType = "int", required = true),
    })
    public Success<List<JdSearchHistory>> getSelectList(Integer type, Integer userId) {
        List<JdSearchHistory> list = jdSearchHistoryService.getSearch(type, userId);
        return Success.success(list);
    }

    /**
     * 清空用户的搜索历史
     */
    @Login
    @PostMapping("/clearSelectList")
    @ApiOperation(value = "清空的搜索历史", notes = "获取用户的搜索历史")
    public Success<String> clearSelectList(
            @ApiIgnore @LoginUser JdAppUser user
    ) {
        jdSearchHistoryService.deleteByUserId(Integer.valueOf(user.getId()));
        return Success.success();
    }

    /**
     * 获取热门搜索新闻列表
     *
     * @return Success
     */
    @GetMapping("/getHotNews")
    @ApiOperation(value = "获取热门搜索新闻列表", notes = "获取热门搜索新闻列表")
    @ApiOperationSupport(order = 4)
    public Success<List<String>> getHotNews() {
        List<String> hotNews = jdSearchHistoryService.getHotNews();
        return Success.success(hotNews);
    }

    /**
     * 获取热门搜索新闻列表
     *
     * @return Success
     */
    @GetMapping("/getHotKeywords")
    @ApiOperation("获取热门搜索关键词")
    public Success<List<String>> getHotKeywords() {
        List<String> hotKeywords = jdSearchHistoryService.getHotKeywords();
        return Success.success(hotKeywords);
    }
}
